from config import *
import random

class DataLoader:
	# constructors
	def __init__(self, cfg = Config()):
		self.cfg = cfg
		self.data = []
		self.labels = []
		self.boundaries = []
		file = open(self.cfg.data_file, 'r')
		for line in file:
			support_list = line.split(',')
			for element_number, element in enumerate(support_list[0:-1]):
				support_list[element_number] = float(element)
			self.data.append(support_list[0:-1])
			self.labels.append(support_list[-1])
		file.close()
		self.get_boundaries()

	#main functions
	def get_training_set(self, amount):
		sample_set = random.sample(range(len(self.data)), amount)
		output = [self.data[i] for i in sample_set]
		self.data = [self.data[i] for i in range(len(self.data)) if i not in sample_set]
		output_labels = [(self.labels[i]).replace('\n','') for i in sample_set]
		self.labels = [self.labels[i] for i in range(len(self.labels)) if i not in sample_set]
		return (output, output_labels)

	#secondary functions
	def get_boundaries(self):
		support_list = [[] for i in range(len(self.data[0]))]
		for vector in self.data:
			for item_number, item in enumerate(vector):
				support_list[item_number].append(item)
		for item_number, item in enumerate(support_list):
			self.boundaries.append((min(item), max(item), max(item) - min(item)))

	def encode_pattern(self, pattern):
		b = self.boundaries
		relative_pattern = [(x - b[i][0]) / b[i][2]  for i,x in enumerate(pattern)]
		return relative_pattern